<template>
  <el-form :model="registerForm" :rules="rules" ref="registerForm" class="login-container" label-position="left"
           label-width="0px">
    <h3 class="login_title">账号注册</h3>
    <el-form-item prop="userName">
      <el-input type="text" v-model="registerForm.userName" auto-complete="off" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input type="password" v-model="registerForm.password" auto-complete="off" placeholder="密码" show-password>
      </el-input>
    </el-form-item>
    <el-form-item style="width: 100%">
      <el-button type="primary" style="width: 100%;background: #505458;border: none" @click="register()">注册
      </el-button>
    </el-form-item>
    <el-form-item>已有账号？<el-button type="text" style="color:#1a5b59;font-weight:bold" @click="login()">登录</el-button></el-form-item>
  </el-form>
</template>

<script>
import api from '@/apis/api';
export default {
  name:'register',
  data() {
    return {
      time: 0,
      rules: {
        //自定义验证规则
        userName: [{
          required: true,
          message: '用户名不能为空',
          trigger: 'blur'
        },{
          pattern: /^[A-Za-z0-9]+$/,    //正则校验不用字符串
          message: "用户名不合法",
          trigger: "blur"
        },{
          min: 8, max: 15,
          message: '长度在 8 到 15 个字符',
          trigger: 'blur'
        }],
        password: [{
          required: true,
          message: '密码不能为空',
          trigger: 'blur'
        },{
          pattern: /^[A-Za-z0-9]+$/,    //正则校验不用字符串
          message: "密码不能包含特殊字符",
          trigger: "blur"
        },{
          min: 6, max: 18,
          message: '长度在 6 到 18 个字符',
          trigger: 'blur'
        }],
        phoneNumber: [{
          required: true,
          message: '请输入手机号',
          trigger: 'blur'
        },{
          pattern: /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/,    //正则校验不用字符串
          message: "请填写正确的手机号码",
          trigger: "blur"
        }],
        verCode: [{
          required: true,
          message: '请输入验证码',
          trigger: 'blur'
        },{
          pattern: /^\d{6}$/,
          message: "验证码为6位数字",
          trigger: "blur"
        }]
      },
      checked: true,
      interval: '', //计时器
      //表单
      registerForm: {
        userName: '',
        password: ''
      },
      loading: false
    }
  },
  mounted() {
    //监听键盘事件
    window.addEventListener('keydown', this.keyDown);
  },
  methods: {
    register(){
      this.$refs.registerForm.validate(valid=>{
        if (valid){
          let  data = {
            userName:this.registerForm.userName,
            password:this.registerForm.password
          }
          api.register(data).then(response=>{
            if (response.code === 200) {
              // 注册成功提示，跳转登录界面
              this.$util.successMessage(response.message);
              this.login();
            }
          })
        }
      })
    },
    //向父组件传参，切换组件状态
    login(){
      this.$emit("status",'login')
    },
    // 回车按钮调用注册方法
    keyDown(e) {
      if (e.keyCode === 13) {
        this.register()
      }
    },
  },
}
</script>
